package com.zysc.processflow.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zysc.processflow.common.base.PageResult;
import com.zysc.processflow.entity.Brand;
import com.zysc.processflow.entity.Sku;
import com.zysc.processflow.entity.Spu;
import com.zysc.processflow.entity.SpuDetail;
import com.zysc.processflow.mapper.SpuMapper;
import com.zysc.processflow.pojo.SpuBo;
import com.zysc.processflow.service.impl.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Mr.Liuyijiang
 * @Description:
 * @Date Created in 2022-06-09 10:07
 * @Modified By:
 */
@Controller
@RequestMapping("/item/")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private SpuMapper spuMapper;



    /**
     * 查询SPU
     * @param key
     * @param saleable
     * @param page
     * @param rows
     * @return
     */
    @GetMapping("spu/page")//key=&saleable=true&page=1&rows=5
    public ResponseEntity<PageResult<SpuBo>> querySpuBypage(
            @RequestParam(value = "key",required = false) String key,
            @RequestParam(value = "saleable",required = false) Boolean saleable,
            @RequestParam(value = "page",defaultValue = "1") int page,
            @RequestParam(value = "rows",defaultValue = "5") int rows
    ){
        List <SpuBo> list = new ArrayList<>();
        if(page>0 && rows>0){
            Page<Spu> spuPage = new Page<Spu>(page,rows);
            list = this.goodsService.querySpuByPage(spuPage,key, saleable);
            return ResponseEntity.ok(new PageResult(spuPage.getTotal(),list));
        }else{
            list = this.goodsService.querySpuByPage(null,key, saleable);
            return ResponseEntity.ok(new PageResult((long)list.size(),list));
        }
    }


    /**
     * 保存新增商品
     * @param spuBo
     * @return
     */
    @PostMapping("goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuBo spuBo){
        this.goodsService.saveGoods(spuBo);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    /**
     * 保存修改商品
     * @param spuBo
     * @return
     */
    @PutMapping("goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuBo spuBo){
        this.goodsService.updateGoods(spuBo);
        return ResponseEntity.noContent().build();//204表示更新和删除成功
    }

    /**
     * 根据spuid查询spuDetail
     * @param spuId
     * @return
     */
    @GetMapping("spu/detail/{spuId}")
    public  ResponseEntity<SpuDetail> querySpuDetailBySpuId(@PathVariable("spuId") Long spuId){
        SpuDetail spuDetail = this.goodsService.querySpuDetailBySpuId(spuId);
        if(spuDetail == null){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(spuDetail);
    }

    /**
     * 根据spuid查询sku的集合
     * @param id
     * @return
     */
    @GetMapping("sku/list")
    public  ResponseEntity<List<Sku>> querySkusBySpuId(@RequestParam("id") Long id){
        List<Sku> skus = this.goodsService.querySkusBySpuId(id);
        if(CollectionUtils.isEmpty(skus)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(skus);
    }

    /**
     * 删除spu
     * @param id
     * @return
     */
    @GetMapping("spu/deleteById")
    public  ResponseEntity deleteById(@RequestParam("id") Long id){
        int i = this.spuMapper.deleteById(id);
        return ResponseEntity.ok(i);
    }

    @PostMapping("spu/updateSpu")
    public  ResponseEntity updateSpu(@RequestBody Spu spu){
        int i = this.spuMapper.updateById(spu);
        return ResponseEntity.ok(i);
    }
}
